﻿
using Dapper;

using HBase;
using hconnect;
using MongoDB.Driver.Core.Clusters;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Diagnostics;
using System.Linq;
using System.Reflection.Emit;
using System.Reflection.Metadata;
using System.Transactions;

namespace hCommand {
    /**
   *┌────────────────────────────────────────────────┐
   *│　描    述：Component                                                    
   *│　作    者：sgj                                              
   *│　版    本：1.0                                              
   *│　创建时间：2020/9/2 11:28:16                        
   *└────────────────────────────────────────────────┘
   **/
    public abstract class AbsTaskComp : AbsLocComp {

        public TaskResDto createTask(  int from, int des, string relyId = "") {
            long taskId = InsertTask(0, from, des,   0, ""); 
            long paramId = moveCoreTaskParam(  taskId, from, des, relyId);
            return new TaskResDto() { taskId = taskId, paramId = paramId };
        }


        public TaskResDto createTask(  AbsCoreStock cs, int des, string relay = "") {
          
            long taskId = InsertTask(cs.deepDiff, cs.locId, des,   cs.id, cs.boxCode);
            long paramTaskId = moveCoreTaskParam(  taskId, cs.locId, des, relay);
            return new TaskResDto() { taskId = taskId, paramId = paramTaskId };
        } 
       
        public virtual int taskBussType(long stockId) {
            return 0;
        }



        protected long InsertTask(long goodDiff, int start, long des, long stockId, string boxCode) {

            CoreTask task = new CoreTask();

            task.stockId = stockId;
            task.boxCode = boxCode;
            task.Status = (int)getStatus(stockId);
            task.bussType = taskBussType(stockId);
            task.src = start;
            task.des = (int)des;

            task.waveNo = getWaveNO(stockId);
            task.goodDiff = goodDiff;
            task.info = createCoreTaskInfo(task);
            insert(task);
            return task.id;
        }
        virtual protected  String getWaveNO(long stockId) {
            return "";
        }
        virtual protected string createCoreTaskInfo(CoreTask task) {

            return string.Format("从{0}到{1}", stnToCrane(task.src), stnToCrane(task.des));
        }
        protected virtual CoreTaskStatus getStatus(long stockId) {
            return CoreTaskStatus.执行中;
        }

    
        protected virtual string stnToCrane(long loc) {
            if (I18nConfig.isLoc(loc)) {
                return (loc + 10000) / 20000 + "号垛机" + loc;
            } else {
                return loc.ToString();
            }
        }
        public long singleParam( long taskId, int src, int des, string relyId, String cName,int cNo, TaskParamType type ) { 

            return this.createParam(taskId, src, des, relyId, cName, cNo, des, type);  
        }
        public long createParam(long taskId, int src, int des, string relyId, String cName="", int cNo=0,int cdes=0, TaskParamType type = TaskParamType.托盘搬运) {
            //任务明细添加
            CoreTaskParam param = new CoreTaskParam(); 
            param.taskId = taskId; //task.id
            param.param = paramInfo(taskId);
            param.src = src; 
            param.des = des;
            param.status = newParamStatus(taskId);//新建状态
            param.type = (byte)type;
            param.relyId = relyId;
            param.taskNo = TaskNoUtil.getTaskNo(this);
            param.cName = cName;
            param.cNo = cNo;
            param.cStn = src;
            param.cDes = cdes;
            insert(param);
            return param.id;
        }

        protected virtual int newParamStatus(long taskId) {
            return (int) CoreTaskParamStatus.可以接收任务;
        }
        protected virtual String paramInfo(long taskId) {
            return "";
        }
      


        public long moveCoreTaskParam(  long taskId, int src, int des) {
            //任务明细添加
            return createParam(  taskId, src, des, "");
        }
        public long moveCoreTaskParam(  long taskId, int src, int des, string relyId) {
            //任务明细添加
            return createParam(  taskId, src, des, relyId);
        }
        
     
    




        protected long getRelyParamId( long stockId, CoreWhLoc loc) {
            string sql = "select max(id) from core_task where stock_id=@stockId    ";
            var id = queryUnique<long>(sql, new { stockId });

            string paramSql = "SELECT max(id) FROM  core_task_param  where task_Id = @id";
            long relyId = queryUnique<long>(paramSql, new { id });
            CoreTaskParam param = getById<CoreTaskParam>(relyId);
            if (param.des == loc.id) {
                throw new Exception("浅货位正在入!");
            }
            return relyId;
        }
    }
}

